package com.zhoukx.observer.improve;


import com.zhoukx.observer.CurrentConditions;

import java.util.ArrayList;

/**
 *  1 包干最新的天气信息
 *  2 含有CurrentConditions对象
 *  3  当数据有更新时，就主动的调用
 */
public class WeatherData implements Subject {
    private float temperatrue;
    private float pressure;
    private float humidity;
    //private CurrentConditions currentConditions;
    // 观察者集合
    private ArrayList<ObServer> obServers;

    public WeatherData() {
        obServers = new ArrayList<>();
    }

    public float getTemperature() {
        return temperatrue;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void dataChange() {
         notifyObserver();
    }

    // 单签数据更新时，被调用
    public void setData(float temperature, float pressure, float humidity) {
        this.temperatrue = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();
    }

    // 注册一个观察者
    @Override
    public void registerObserver(ObServer o) {
           obServers.add(o);
    }

    // 移除一个观察者
    @Override
    public void removeObserver(ObServer o) {
        if (obServers.contains(o)) {
            obServers.remove(o);

        }
    }

    // 便利观察者并通知
    @Override
    public void notifyObserver() {
        for (int i = 0; i < obServers.size(); i++) {
            ObServer obServer = obServers.get(i);
            obServer.update(this.temperatrue,this.pressure,this.humidity);
        }

    }
}
